/*
 * 阔地教育科技有限公司版权所有(codyy.com/codyy.cn)
 * Copyright (c) 2020, Codyy and/or its affiliates. All rights reserved.
 */

package com.codyy.lib_common.observer

import android.content.ContentResolver
import android.content.Context
import android.database.ContentObserver
import android.os.Handler
import android.provider.Settings
import com.codyy.lib_common.listener.OrientationSensorManager

/**
 * create by lijian on 2020/01/09
 * 观察屏幕旋转设置变化，类似于注册动态广播监听变化机制
 * 参考:https://www.jianshu.com/p/57e9e3bb3e5d
 */
class RotationObserver(
    handler: Handler,
    context: Context,
    private val orientationSensorManager: OrientationSensorManager
) : ContentObserver(handler) {
    private var mResolver: ContentResolver = context.contentResolver

    //屏幕旋转设置改变时调用
    override fun onChange(selfChange: Boolean) {
        super.onChange(selfChange)
        orientationSensorManager.registerListener()
    }

    fun startObserver() {
        mResolver.registerContentObserver(
            Settings.System
                .getUriFor(Settings.System.ACCELEROMETER_ROTATION), false,
            this
        )
    }

    fun stopObserver() {
        mResolver.unregisterContentObserver(this)
    }
}